package topo.dataview;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import topo.table.ObjectTable;
import topo.table.ObjectTableDesc;
import topo.table.ObjectTableModel;
import topo.table.ScrollTablePane;
import topo.table.TableMouseHandle;
import topo.utils.DialogTools;

import com.zhtelecom.common.snmp.SNMPAPI;
import com.zhtelecom.common.snmp.SNMPFactory;
import com.zhtelecom.common.snmp.SNMPTarget;

public class SNMPTableDialog extends JDialog {
    public static ResourceBundle res = ResourceBundle.getBundle("topo.dataview.DefaultSNMPTableDialogRes");
    public ObjectTableModel mod;
    public ObjectTable table;
    ScrollTablePane jScrollPaneCenter;
    JPanel jPanelSouth = new JPanel();
    public JButton jButtonAdd = new JButton();
    public JButton jButtonDel = new JButton();
    public JButton jButtonQuery = new JButton();
    public JButton jButtonClose = new JButton();
    public JButton jButtonDetails = new JButton();
    boolean showAdd;
    boolean showDel;
    boolean showDetail;
    boolean showQueryAll;
    public ObjectTableDesc tableDesc;
    public SNMPSheetDialog sheetAddDialog;
    public SNMPSheetDialog sheetViewDialog;
    SNMPTarget target;

    public void processAdd() {
        Object mib = getNewAddObject();
        if (this.sheetAddDialog != null) {
            this.sheetAddDialog.initObject(this.target, mib, this.mod);
            DialogTools.WindowPack(this, this.sheetAddDialog);
        }
    }

    public void processDel() {
        Object obj = this.table.getSelectRowObject();
        try {
            SNMPAPI snmpService = SNMPFactory.getSNMPAPI();
            snmpService.delTableRow(obj, this.target);
            this.mod.delRowObject(obj);
            DialogTools.showOkMsg(this, res.getString("String_8"), res.getString("String_9"));
        } catch (Exception ex) {
            DialogTools.showErroMsg(this, res.getString("String_10"));
            ex.printStackTrace();
        }
    }

    public void processQueryall() {
        try {
            SNMPAPI snmpService = SNMPFactory.getSNMPAPI();
            List list = snmpService.getAllTableData(this.tableDesc.getTableObjectClass(), this.target);
            this.mod.clearAllObject();
            this.mod.addRowObject(list);
            if (list.size() > 0) {
                this.table.setRowSelectionInterval(0, 0);
            }
        } catch (Exception ex) {
            DialogTools.showErroMsg(this, res.getString("String_2"));
            ex.printStackTrace();
        }
    }

    public void processClose() {
        dispose();
    }

    public void processView() {
        if (!this.showDetail) {
            return;
        }
        if (this.sheetViewDialog != null) {
            Object obj = this.table.getSelectRowObject();
            this.sheetViewDialog.initObject(this.target, obj, this.mod);
            DialogTools.WindowPack(this, this.sheetViewDialog);
        }
    }

    public SNMPTableDialog(Frame owner, ObjectTableDesc tableDesc, boolean showAdd, boolean showDel, boolean showDetail, boolean showQueryAll) {
        super(owner);
        this.tableDesc = tableDesc;
        this.showAdd = showAdd;
        this.showDel = showDel;
        this.showDetail = showDetail;
        this.showQueryAll = showQueryAll;
        this.mod = new ObjectTableModel(tableDesc);
        this.table = new ObjectTable(this.mod);
        this.jScrollPaneCenter = new ScrollTablePane(this.table);
        try {
            jbInit();
            initTableView();
        } catch (Exception e) {
            e.printStackTrace();
        }
        setDefaultCloseOperation(2);
    }

    public void myInit(SNMPTarget target, SNMPSheetDialog sheetAddDialog, SNMPSheetDialog sheetViewDialog) {
        this.target = target;
        this.sheetAddDialog = sheetAddDialog;
        this.sheetViewDialog = sheetViewDialog;
    }

    Object getNewAddObject() {
        try {
            return this.tableDesc.getTableObjectClass().newInstance();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    private void initTableView() {
        try {
            getContentPane().add(this.jScrollPaneCenter, "Center");
            this.jScrollPaneCenter.setMouseHandle(new Hand());
            ListSelectionListener selAction = new ListSelectionListener() {
                public void valueChanged(ListSelectionEvent e) {
                    Object obj = SNMPTableDialog.this.table.getSelectRowObject();
                    if (obj != null) {
                        SNMPTableDialog.this.jButtonDel.setEnabled(true);
                        SNMPTableDialog.this.jButtonDetails.setEnabled(true);
                    } else {
                        SNMPTableDialog.this.jButtonDel.setEnabled(false);
                        SNMPTableDialog.this.jButtonDetails.setEnabled(false);
                    }
                }
            };
            this.table.getSelectionModel().addListSelectionListener(selAction);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.jButtonAdd.setText(res.getString("String_3"));
        ActionListener addAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SNMPTableDialog.this.processAdd();
            }
        };
        ActionListener delAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SNMPTableDialog.this.processDel();
            }
        };
        ActionListener viewAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SNMPTableDialog.this.processView();
            }
        };
        ActionListener queryAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SNMPTableDialog.this.processQueryall();
            }
        };
        ActionListener closeAction = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                SNMPTableDialog.this.processClose();
            }
        };
        this.jButtonAdd.addActionListener(addAction);
        this.jButtonDel.setEnabled(false);
        this.jButtonDel.setText(res.getString("String_4"));
        this.jButtonDel.addActionListener(delAction);
        this.jButtonQuery.setText(res.getString("String_5"));
        this.jButtonQuery.addActionListener(queryAction);
        this.jButtonClose.setText(res.getString("String_6"));
        this.jButtonClose.addActionListener(closeAction);
        this.jPanelSouth.setBorder(BorderFactory.createEtchedBorder());
        this.jPanelSouth.setDebugGraphicsOptions(0);
        this.jButtonDetails.setEnabled(false);
        this.jButtonDetails.setText(res.getString("String_7"));
        this.jButtonDetails.addActionListener(viewAction);
        getContentPane().add(this.jPanelSouth, "South");
        if (this.showAdd) {
            this.jPanelSouth.add(this.jButtonAdd, null);
        }
        if (this.showDel) {
            this.jPanelSouth.add(this.jButtonDel, null);
        }
        if (this.showDetail) {
            this.jPanelSouth.add(this.jButtonDetails, null);
        }
        if (this.showQueryAll) {
            this.jPanelSouth.add(this.jButtonQuery, null);
        }
        this.jPanelSouth.add(this.jButtonClose, null);
    }

    class AddAction extends AbstractAction {
        public void actionPerformed(ActionEvent e) {
            SNMPTableDialog.this.processAdd();
        }

        public AddAction() {
            super();
        }
    }

    class DelAction extends AbstractAction {
        public void actionPerformed(ActionEvent e) {
            SNMPTableDialog.this.processDel();
        }

        public DelAction() {
            super();
        }
    }

    class Hand implements TableMouseHandle {
        Hand() {
        }

        public JPopupMenu createObjectMenu(MouseEvent e) {
            JPopupMenu menu = new JPopupMenu();
            if (SNMPTableDialog.this.showAdd) {
                //                menu.add(new SNMPTableDialog.AddAction(SNMPTableDialog.this));
                menu.add(new SNMPTableDialog.AddAction());
                menu.addSeparator();
            }
            if (SNMPTableDialog.this.showDetail) {
                //                menu.add(new SNMPTableDialog.ViewAction(SNMPTableDialog.this));
                menu.add(new SNMPTableDialog.ViewAction());
            }
            if (SNMPTableDialog.this.showDel) {
                //                menu.add(new SNMPTableDialog.DelAction(SNMPTableDialog.this));
                menu.add(new SNMPTableDialog.DelAction());
            }
            if (SNMPTableDialog.this.showQueryAll) {
                menu.addSeparator();
                //                menu.add(new SNMPTableDialog.QueryAllAction(SNMPTableDialog.this));
                menu.add(new SNMPTableDialog.QueryAllAction());
            }
            return menu;
        }

        public JPopupMenu createBlankMenu(MouseEvent e) {
            JPopupMenu menu = new JPopupMenu();
            if (SNMPTableDialog.this.showAdd) {
                //                menu.add(new SNMPTableDialog.AddAction(SNMPTableDialog.this));
                menu.add(new SNMPTableDialog.AddAction());
            }
            if (SNMPTableDialog.this.showQueryAll) {
                menu.addSeparator();
                //                menu.add(new SNMPTableDialog.QueryAllAction(SNMPTableDialog.this));
                menu.add(new SNMPTableDialog.QueryAllAction());
            }
            return menu;
        }

        public void doubleClickAction(MouseEvent e) {
            SNMPTableDialog.this.processView();
        }
    }

    class QueryAllAction extends AbstractAction {
        public void actionPerformed(ActionEvent e) {
            SNMPTableDialog.this.processQueryall();
        }

        public QueryAllAction() {
            super();
        }
    }

    class ViewAction extends AbstractAction {
        public void actionPerformed(ActionEvent e) {
            SNMPTableDialog.this.processView();
        }

        public ViewAction() {
            super();
        }
    }
}